function kill(packageName){
    log('杀掉进程')
    // 关闭app
    for (let index = 0; index < 6; index++) {
        if (currentPackage() == packageName){
            back();
            log(currentPackage())
            if(currentPackage() != packageName){
                return true
            }
            sleep(300)
        }
    }
}

function judge_home() {
    // 是否进入首页
    for (let index = 0; index < 7; index++) {
        if (!className("android.widget.TextView").text('缘分').exists()) {
            back()
            sleep(3000)
        }else if(className("android.widget.TextView").text('缘分').exists()){
                sleep(3000)
            return;
        }
        sleep(3000)
    }
    kill("com.dlwj.lywb")
    sleep(3000)
    init()
}

function init() {
    record_time = new Date().getTime();
    sleep(3000)
    log('启动来约我吧')
    // 启动来约我吧
    app.startActivity({
        action: "android.intent.action.MAIN",
        packageName: "com.dlwj.lywb",
        className: "com.app.ui.activity.WelcomeActivity",
        category: ["android.intent.category.LAUNCHER"],
        flags: ["activity_new_task"]
    });
    record_time = new Date().getTime();
    for (let index = 0; index < account_list.length; index++) {
        element = account_list[index];
        account = element.split("----")
        record_time = new Date().getTime();
        sleep(10000)
        record_time = new Date().getTime();
        log('运行')
        // 是否进入首页
        judge_home()
        log('成功进入首页');
        record_time = new Date().getTime();
        // 选定分类
        sleep(3000)
        log('进入我页面')
        className('android.widget.RadioButton').clickable(true).text('我').findOne().click()
        sleep(3000)
        record_time = new Date().getTime();
        log('进入资料页面')
        textStartsWith('资料').visibleToUser().exists()
        textStartsWith('资料').visibleToUser().className('android.widget.TextView').clickable(true).findOne().click()
        sleep(3000)
        record_time = new Date().getTime();
        log('设置昵称')
        text('昵称').findOne().parent().parent().click()
        sleep(3000)
        log('输入昵称')
        setText(name_list[random(0, name_list.length-1)])
        sleep(3000)
        record_time = new Date().getTime();
        log('确定')
        className('android.widget.Button').text('确定').clickable(true).findOne().click()
        sleep(3000)
        log('保存')
        className('android.widget.Button').text('保存').clickable(true).findOne().click()
        sleep(3000)
        record_time = new Date().getTime();
        log('滑动底部')
        scrollForward()
        sleep(3000)
        log('进入设置页面')
        className('android.widget.TextView').text('设置').findOne().parent().click()
        sleep(3000)
        record_time = new Date().getTime();
        log('切换用户页面')
        className('android.widget.Button').text('切换用户').clickable(true).findOne().click()
        sleep(3000)
        log('确定')
        className('android.widget.Button').text('确定').clickable(true).findOne().click()
        sleep(3000)
        record_time = new Date().getTime();
        log('输入账号密码')
        setText(0,account[0])
        setText(1,account[1])
        record_time = new Date().getTime();
        sleep(3000)
        log('登录')
        className('android.widget.Button').text('登录').clickable(true).findOne().click()
        sleep(3000)
        record_time = new Date().getTime();
        //className('android.widget.Button').text('登录').clickable(true).findOne().click()
    }
    log('全部完成')
}


auto.waitFor();  // 无障碍服务是否已经启用

console.show()  // 控制台

var storage = storages.create("laiyuewoba");

var name_list = storage.get("name_list");
name_list = name_list.split(",")
var account_list = storage.get("account_list");
account_list = account_list.split(",")
log(account_list)
record_time = new Date().getTime();
var play = threads.start(init);

while (true) {
    if (new Date().getTime()-record_time > 120000) {
        log('当前时间差:'+String(new Date().getTime()-record_time))
        log('杀死主线程')
        play.interrupt();
        record_time = new Date().getTime();   
        sleep(30000)
        play = threads.start(init);
    } else {
        log('尚未卡死，休息休息30s后再检测')
        // log('当前时间差大于120000即为卡死')
        log('当前时间差:'+String(new Date().getTime()-record_time))
        sleep(30000)
    }
}



/*
827635395----643551
827635338----193793
827635312----980119
*/
